package com.sgl.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgl.blog.mapper.ArticleMapper;
import com.sgl.blog.pojo.BlogArticle;
import com.sgl.blog.service.AccountService;
import com.sgl.blog.service.ArticleService;
import com.sgl.blog.service.LabelService;
import com.sgl.blog.service.TypeService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 简介
 * 作者： 李志伟
 * 日期： 2020/4/3
 * 时间： 20:29
 */
@Service
public class ArticleServiceImpl implements ArticleService {


    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private AccountService accountService;

    @Resource
    private TypeService typeService;

    @Resource
    private LabelService labelService;


    @Override
    public Boolean insertBlog(BlogArticle article) {
        System.out.println(article);
        return articleMapper.insert(article) > 0;
    }

    @Override
    public Boolean updateBlog(BlogArticle article) {
        return articleMapper.updateByPrimaryKey(article) > 0;
    }

    @Override
    public Boolean removeBlog(BlogArticle blogArticle) {
        return articleMapper.delete(blogArticle) > 0;
    }

    @Override
    public Map<String, Object> findBlogById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        BlogArticle blogArticle = articleMapper.selectByPrimaryKey(id);
        if (blogArticle.getArticleState() == 1){
            map.put("article",blogArticle);
            map.put("account",accountService.findById(blogArticle.getAccountId()));
            map.put("type",typeService.findById(blogArticle.getTypeId()));
            map.put("blogLabels",labelService.findByStringId(blogArticle.getLabelIds()));
            map.put("typeHots", findHotBlogByType(blogArticle.getTypeId()));
        } else {
            map.put("article", blogArticle);
        }
        return map;
    }

    @Override
    public PageInfo<BlogArticle> findBlogByPage(Integer page) {
        // 创建 Example对象 负责条件查询
        Example example = new Example(BlogArticle.class);
        example.selectProperties("articleId", "articleTitle", "articleDescription", "articleFirstPicture", "articleCreateTime").createCriteria().andEqualTo("articleState",1);
        // 按最新时间查询
        example.setOrderByClause("article_create_time desc");

        // 分页
        PageHelper.startPage(page, 8);

        return  new PageInfo<>(articleMapper.selectByExample(example));
    }

    @Override
    public List<BlogArticle> findBlogByAccount(Integer accountId) {
        Example example = new Example(BlogArticle.class);
        example.selectProperties("articleId","articleTitle","articleCommentCount","articleCreateTime","articleLikeCount","articleState")
                .createCriteria()
                .andEqualTo("accountId",accountId);
        return articleMapper.selectByExample(example);
    }

    @Override
    public void likesCountPlus(Integer id) {
        articleMapper.articleLikesCountPlus(id);
    }

    @Override
    public void likesCountSub(Integer id) {
        articleMapper.articleLikesCountSub(id);
    }

    @Override
    public List<BlogArticle> findHotBlogByType(Integer typeId) {
        Example example = new Example(BlogArticle.class);
        Example.Criteria criteria = example.createCriteria();
        example.getPropertyMap();
        example.selectProperties("articleId","articleTitle","articleDescription","articleFirstPicture","typeId");
        criteria.andEqualTo("typeId",typeId);
        criteria.andEqualTo("articleState",0);
        example.setOrderByClause("article_like_count DESC LIMIT 6");
        return articleMapper.selectByExample(example);
    }

    @Override
    public void commentCountPlus(Integer id) {
        articleMapper.articleCommentCountSub(id);
    }

    @Override
    public void updateState(Integer id) {
        articleMapper.updateState(id);
    }
}